﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace NeroConnect.Network
{
	class LogStream : Stream
	{
		private void Log(string message)
		{
			// Trace.TraceInformation("STREAM - THREAD: " + System.Threading.Thread.CurrentThread.GetHashCode() + " - Stream: " + ((_s != null) ? _s.ToString() : "") + " - " + message);
		}

		Stream _s;
		public LogStream(Stream s)
		{
			_s = s;
			Log("LogStream()");
		}
		public override bool CanRead
		{
			get
			{
				Log("get_CanRead");
				return _s.CanRead;
			}
		}

		public override bool CanSeek
		{
			get
			{
				Log("get_CanSeek");
				return _s.CanSeek;
			}
		}

		public override bool CanWrite
		{
			get
			{
				Log("get_CanWrite");
				return _s.CanWrite;
			}
		}

		public override void Flush()
		{
			Log("Flush()");
			_s.Flush();
		}

		public override long Length
		{
			get
			{
				Log("get_Length");
				return _s.Length;
			}
		}

		public override long Position
		{
			get
			{
				Log("get_Position");
				return _s.Position;
			}
			set
			{
				Log(string.Format("set_Position({0})", value));
				_s.Position = value;
			}
		}

		public override int Read(byte[] buffer, int offset, int count)
		{
			Log(string.Format("Read(offset={0}, count={1})", offset, count));
			return _s.Read(buffer, offset, count);
		}

		public override long Seek(long offset, SeekOrigin origin)
		{
			Log(string.Format("Seek(offset={0}, origin={1})", offset, origin));
			return _s.Seek(offset, origin);
		}

		public override void SetLength(long value)
		{
			Log(string.Format("SetLength({0})", value));
			_s.SetLength(value);
		}

		public override void Write(byte[] buffer, int offset, int count)
		{
			Log(string.Format("Write(offset={0}, count={1})", offset, count));
			_s.Write(buffer, offset, count);
		}

		public override bool CanTimeout
		{
			get
			{
				Log("get_CanTimeout");
				return _s.CanTimeout;
			}
		}
		public override int ReadTimeout
		{
			get
			{
				Log("get_ReadTimeout");
				return _s.ReadTimeout;
			}
			set
			{
				Log(string.Format("put_ReadTimeout({0}", value));
				_s.ReadTimeout = value;
			}
		}
		public override int WriteTimeout
		{
			get
			{
				Log("get_WriteTimeout");
				return _s.WriteTimeout;
			}
			set
			{
				Log(string.Format("put_WriteTimeout({0}", value));
				_s.WriteTimeout = value;
			}
		}

		public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			Log(string.Format("BeginRead(offset={0}, count={1})", offset, count));
			return _s.BeginRead(buffer, offset, count, callback, state);
		}

		public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
		{
			Log(string.Format("BeginWrite(offset={0}, count={1})", offset, count));
			return _s.BeginWrite(buffer, offset, count, callback, state);
		}

		public override void Close()
		{
			Log("Close()");
			_s.Close();
		}

		public override int EndRead(IAsyncResult asyncResult)
		{
			Log("EndRead()");
			return _s.EndRead(asyncResult);
		}

		public override void EndWrite(IAsyncResult asyncResult)
		{
			Log("EndWrite()");
			_s.EndWrite(asyncResult);
		}

		public override int ReadByte()
		{
			Log("ReadByte()");
			return _s.ReadByte();
		}

		public override void WriteByte(byte value)
		{
			Log(string.Format("WriteByte({0})", value));
			_s.WriteByte(value);
		}
	}
}
